package com.weirq.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.weirq.dao.BaseDao;
import com.weirq.util.DateUtil;
import com.weirq.vo.ShareFile;
import com.weirq.vo.UserModel;

/**
 * 
 * @author pc-Haitao
 * @ClassName:BaseDaoImpl BaseDao接口的实现类
 * @Version 版本
 * @Modifiedby 修改人
 * @Copyright 公司名称
 * @date 2016年8月18日下午4:34:52
 */
@Repository("baseDao")
public class BaseDaoImpl implements BaseDao {

	private static JdbcTemplate jdbc;

	public static void setJdbcTemplate(JdbcTemplate jdbcTemplate) {// 依赖注入 jdbc
		BaseDaoImpl.jdbc = jdbcTemplate;
	}

	@Override
	public void save(UserModel userModel) {// 将注册用户名及密码，保存至数据库user表
		String id = userModel.getId();
		String username = userModel.getUsername();
		String password = userModel.getPassword();
		String email = userModel.getEmail();
		int status = userModel.getStatus();
		String validateCode = userModel.getValidateCode();
		String registerTime = DateUtil.DateToString("yyyy-MM-dd HH:mm:ss", userModel.getRegisterTime());
		String sql = "INSERT INTO user (id,username,password,email,status,validateCode,registerTime) " + "VALUES ('" + id + "','" + username + "','" + password + "','" + email + "','" + status + "','" + validateCode + "','" + registerTime + "');";
		jdbc.execute(sql);// 执行sql语句
	}

	@Override
	public void add(ShareFile shareFile) {// 保存分享信息
		String id = shareFile.getId();
		String username = shareFile.getUsername();
		String fileName = shareFile.getFileName();
		String ecode = shareFile.getEcode();
		String link = shareFile.getLink();
		String time = DateUtil.DateToString("yyyy-MM-dd HH:mm:ss", shareFile.getTime());
		String sql = "INSERT INTO share_file (id,user_name,file_name,ecode,link,time) " + "VALUES ('" + id + "','" + username + "','" + fileName + "','" + ecode + "','" + link + "','" + time + "');";
		jdbc.execute(sql);// 执行sql语句
	}

	@Override
	public List<Map<String, Object>> findbysharename(String username) {// 根据分享用户名查找数据库share_file表
		String sql = "SELECT * from share_file where user_name='" + username + "'";
		List<Map<String, Object>> li = jdbc.queryForList(sql);
		return li;
	}

	@Override
	public List<Map<String, Object>> findByEcode(String id, String ecode) {//根据分享id和提取码查找数据库share_file表
		String sql = "SELECT * from share_file where id='" + id + "' and ecode='" + ecode + "'";
		List<Map<String, Object>> li = jdbc.queryForList(sql);
		return li;
	}

	@Override
	public void deleteshare(String dir) {
		String sql = "DELETE from share_file where file_name='" + dir + "'";// 根据文件名删除数据库share_file表
		jdbc.execute(sql);// 执行sql语句
	}

	@Override
	public List<Map<String, Object>> find(String username, String password) {// 根据用户名和密码查找数据库user表
		String sql = "SELECT * from user where username='" + username + "' and password='" + password + "'";
		List<Map<String, Object>> li = jdbc.queryForList(sql);
		return li;
	}

	@Override
	public List<Map<String, Object>> findbyname(String username) {// 根据注册用户名查找数据库user表
		String sql = "SELECT * from user where username='" + username + "'";
		List<Map<String, Object>> li = jdbc.queryForList(sql);
		return li;
	}

	@Override
	public void updateStatus(String email, int status) {// 更新注册用户状态
		String sql = "UPDATE user set status='" + status + "'where email='" + email + "'";
		jdbc.execute(sql);// 执行sql语句
	}

	@Override
	public void updatePassword(String email, String password) {// 更新注册用户密码
		String sql = "UPDATE user set password='" + password + "'where email='" + email + "'";
		jdbc.execute(sql);// 执行sql语句
	}

	@Override
	public List<Map<String, Object>> findbyemail(String email) {// 根据邮箱查找数据库
		String sql = "SELECT * from user where email='" + email + "'";
		List<Map<String, Object>> li = jdbc.queryForList(sql);
		return li;
	}
}
